﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using FlickrNet;

namespace FlickrPortfolio.Models
{
    public class Photo
    {
        public string ID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public IList<PhotoUrl> Urls { get; set; }
        public IList<Exif> Exif { get; set; }
        public IList<Tag> Tags { get; set; }
        
        public Photo(PhotoInfo photo) {
            ID = photo.PhotoId;
            Title = photo.Title;
            Description = photo.Description;

            Urls = new List<PhotoUrl> {
                                          new PhotoUrl(photo.ThumbnailUrl, UrlType.Thumbnail),
                                          new PhotoUrl(photo.SmallUrl, UrlType.Small),
                                          new PhotoUrl(photo.MediumUrl, UrlType.Medium),
                                          new PhotoUrl(photo.LargeUrl, UrlType.Large),
                                          new PhotoUrl(photo.OriginalUrl, UrlType.Original)
                                      };

            Exif = new List<Exif>();
            foreach (var item in photo.ExifTagCollection)
                Exif.Add(new Exif { CleanName = item.Clean, RawName = item.Raw, Label = item.Label, Tag = item.Tag });
            
            Tags = new List<Tag>();
            foreach (var item in photo.Tags.TagCollection)
                Tags.Add(new Tag { ID = item.TagId, RawName = item.Raw, Text = item.TagText });
        }

        public Photo(FlickrNet.Photo photo)
        {
            ID = photo.PhotoId;
            Title = photo.Title;

            //No Description property in the FlickrNet Photo class

            Urls = new List<PhotoUrl>();

            try {
                Urls.Add(new PhotoUrl(photo.OriginalUrl, UrlType.Original));
            } catch { }

            try {
                Urls.Add(new PhotoUrl(photo.LargeUrl, UrlType.Large));
            } catch { }
            
            try {
                Urls.Add(new PhotoUrl(photo.MediumUrl, UrlType.Medium));
            } catch { }
            
            try {
                Urls.Add(new PhotoUrl(photo.SmallUrl, UrlType.Small));
            } catch { }
            
            try {
                Urls.Add(new PhotoUrl(photo.ThumbnailUrl, UrlType.Thumbnail));
            } catch { }
            
            //No EXIF reference in the FlickrNet Photo class
            //foreach (var item in photo)

            //Ugly Tags reference in the FlickrNet Photo class
        }
    }
}
